package com.qn.wechat.common;


import org.apache.commons.lang3.StringUtils;
import org.dozer.DozerConverter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MapperStringToDateConverter extends DozerConverter<String, Date> {
    public MapperStringToDateConverter() {
        super(String.class, Date.class);
    }

    public Date convertTo(String source, Date destination) {
        if (StringUtils.isBlank(source)) {
            return null;
        } else if (StringUtils.isNumeric(source)) {
            return new Date(Long.parseLong(source));
        } else {
            SimpleDateFormat sdf = null;
            if (source.indexOf(":") > 0) {
                sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            } else {
                sdf = new SimpleDateFormat("yyyy-MM-dd");
            }

            try {
                return sdf.parse(source);
            } catch (ParseException var5) {
                var5.printStackTrace();
                return null;
            }
        }
    }

    public String convertFrom(Date source, String destination) {
        if (source == null) {
            return null;
        } else {
            String result = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(source);
            return result;
        }
    }
}
